package org.dtrd.modules.suggest.controller;


import cn.hutool.core.util.StrUtil;
import org.dtrd.common.system.util.JwtUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.suggest.entity.bean.DoctorSigninSuggestCategoryDetail;
import org.dtrd.modules.suggest.entity.bean.DoctorSigninSuggestCategoryListInfo;
import org.dtrd.modules.suggest.entity.bean.DoctorSigninSuggestCategoryListRequest;
import org.dtrd.modules.suggest.entity.bean.DoctorSigninSuggestCategoryPageRequest;
import org.dtrd.modules.suggest.service.IDtrdEntRdDoctorSigninSuggestCategoryService;
import org.dtrd.common.api.vo.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import org.dtrd.base.BaseController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * <p>
 * 医生端打卡建议分类表 前端控制器
 * </p>
 * @author sun
 * @since 2022-09-21
 */
@RestController
@RequestMapping("/suggest/category")
@Api(tags = "话术库分类")
public class DtrdEntRdDoctorSigninSuggestCategoryController extends BaseController {

    @Autowired
    private IDtrdEntRdDoctorSigninSuggestCategoryService doctorSigninSuggestCategoryService;

    @ApiOperation(value = "查询医生端打卡建议分类表列表", response = DoctorSigninSuggestCategoryListInfo.class)
    @PostMapping("/list")
    @AutoLog(value="查询医生端打卡建议分类表列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getDoctorSigninSuggestCategoryList(@RequestBody @Validated DoctorSigninSuggestCategoryListRequest request){
        List<DoctorSigninSuggestCategoryListInfo> list = doctorSigninSuggestCategoryService.getDoctorSigninSuggestCategoryList(request);
        return Result.OK(list);
    }

    @ApiOperation("新增或修改医生端打卡建议分类表")
    @PostMapping("/save")
    @AutoLog(value="新增或修改医生端打卡建议分类表", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateDoctorSigninSuggestCategory(@RequestBody @Validated DoctorSigninSuggestCategoryDetail dto, HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("X-Access-Token");
        String username = null;
        if (StrUtil.isNotBlank(token)) {
            username = JwtUtil.getUsername(token);
        }
        boolean result = doctorSigninSuggestCategoryService.saveOrUpdateDoctorSigninSuggestCategory(dto, username);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除医生端打卡建议分类表")
    @DeleteMapping("/delete")
    @AutoLog(value="删除医生端打卡建议分类表", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeDoctorSigninSuggestCategory(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = doctorSigninSuggestCategoryService.removeDoctorSigninSuggestCategory(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
